import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
import { getTenantId } from '/@/utils/auth';

const { createConfirm } = useMessage();

enum Api {
  list = '/submission/list',
  save='/submission/add',
  edit='/submission/edit',
  deleteOne = '/submission/delete',
  deleteBatch = '/submission/deleteBatch',
  importExcel = '/submission/importExcel',
  exportXls = '/submission/exportXls',
  getOne = '/submission/queryById'
}

const checkAuth = () => {
  return getTenantId() ? '/submission/listLocalTestOrgData' : Api.list
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params: any) =>
  defHttp.get({url: Api.list, params});

export const list2 = (params: any) =>
  defHttp.get({url: '/submission/listLocalTestOrgData', params});

/**
 * 详情接口
 * @param params
 */
export const getOne = (params: { id: any; }) =>
  defHttp.get({url: Api.getOne, params});

/**
 * 列表接口
 * @param params
 */
export const orgList = (params: any) =>
  defHttp.get({url: '/testOrg/list', params});

/**
 * 删除单个
 */
export const deleteOne = async (params,handleSuccess) => {
  await defHttp.post({ url: Api.deleteOne, params }, { joinParamsToUrl: true });
  handleSuccess();
}
/**
 * 送检单个
 */
export const checkOne = async (params,handleSuccess) => {
  await defHttp.post({ url: '/submission/submission', params }, { joinParamsToUrl: true });
  handleSuccess();
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.post({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}
/**
 * 保存或者更新机构
 * @param params
 */
export const addOrg = (params: any) => {
  return defHttp.get({url: "/testOrg/add", params});
}
